package com.kintreda.ieco_server.module.qrcode.service.impl;

import com.kintreda.ieco_server.bean.goods.Sack;
import com.kintreda.ieco_server.bean.qrcode.batch;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.order.service.ISackService;
import com.kintreda.ieco_server.module.qrcode.service.IBatchService;
import com.kintreda.ieco_server.util.DateUtil;
import com.kintreda.ieco_server.util.QRCode.QrCode;
import com.kintreda.ieco_server.util.QRCode.ZXingCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.qrcode.service.impl
 * @Author ZhongYao_H
 * @Date 2019/4/30:10:45
 * @Year 2019 Month 04 Day 30
 * @Date_zh 2019 年 四月 星期二
 */
@Service
public class BatchServiceImpl extends BaseServiceImpl<batch,String> implements IBatchService {

    @Autowired
    private ISackService sackService;
    @Value("#{properties['qrcode.path']}")
    private String qrcodePath;

    @Override
    public batch getBatch(String batch) {
        List<batch> query = super.query("from batch where code = ?", new String[]{batch});
        return query.size()>0?query.get(0):null;
    }


    @Override
    public Map<String,Object> generateCodes(String domain, String url, String prefix, String batch, int num, int numberDB, boolean bat) {
        if (bat==true){
            numberDB=numberDB+5;
        }
        List<Sack> sacks = new LinkedList<>();
        prefix = prefix+batch;
        url = domain+url;
        int start = 0 ;
        String time = new Date().getTime()+"";
        String date = DateUtil.getDate(new Date(), DateUtil.YYYYMMDD);
        for (int i = 0; i < num; i++) {
            if (bat==true){
                //已有批次 继续生成
                start = numberDB++;
            }else {
                start++;
            }
            String code = prefix+supplement(start+"");
            String codeurl= url+"?code="+code;
            String path = qrcodePath+ date+File.separator+time;
            String fileName = code+".jpg";
            QrCode.createQrCode(codeurl, path, fileName,code);

            Sack sack = new Sack();
            sack.setQrcode(code);
            sack.setStatus("0");
            sacks.add(sack);
        }
        sackService.saveOrUpdateAll(sacks);

        Map<String,Object> result = new HashMap<>();
        result.put("pfolder",date);
        result.put("folder",time);
        result.put("num",sacks.size());
        result.put("batch",batch);
        return result;

    }


    @Override
    public Map<String, Object> generateKimsVolume(String domain, String url, String prefix, String batch, int num, int numberDB, boolean bat, String log) {
        if (bat==true){
            numberDB=numberDB+5;
        }
        List<Sack> sacks = new LinkedList<>();
        prefix = prefix+batch;
        url = domain+url;
        int start = 0 ;
        String time = new Date().getTime()+"";
        String date = DateUtil.getDate(new Date(), DateUtil.YYYYMMDD);
        for (int i = 0; i < num; i++) {
            if (bat==true){
                //已有批次 继续生成
                start = numberDB++;
            }else {
                start++;
            }
            String code = prefix+supplement(start+"");
            String codeurl= url+"?code="+code;
            String path = qrcodePath+ date+File.separator+time;
            File folder = new File(path);
            if (!folder.exists()){
                folder.mkdirs();
            }
            String fileName = code+".jpg";
//            QrCode.createQrCode(codeurl, path, fileName,code); 无log
            ZXingCode.getLogoQRCode(codeurl,log,code,new File(path+File.separator+fileName));//带log

            Sack sack = new Sack();
            sack.setQrcode(code);
            sack.setStatus("3");
            sacks.add(sack);
        }
        sackService.saveOrUpdateAll(sacks);

        Map<String,Object> result = new HashMap<>();
        result.put("pfolder",date);
        result.put("folder",time);
        result.put("num",sacks.size());
        result.put("batch",batch);
        return result;
    }

    @Override
    public String supplement(String code){
        String supp = "";
        switch (code.length()){
            case 0:
                break;
            case 1:
                supp="0000";
                break;
            case 2:
                supp="000";
                break;
            case 3:
                supp="00";
                break;
            case 4:
                supp="0";
                break;
            case 5:
                break;
            default:
                    break;
        }

        return supp+code;

    }




}
